#region Usings

using System.Text;
using System.Xml;
using System.Xml.XPath;

#endregion

namespace Kazkommertsbank.Framework.Model.Pay
{
    public class PaymentOrder
    {
        //private const string FORMAT = "<order order_id=\"{0}\" amount=\"{1}\" currency=\"{2}\"><department merchant_id=\"{3}\" amount=\"{4}\"/></order>";

        public PaymentOrder()
        {}

        public PaymentOrder(XPathNavigator nav)
        {
            OrderId = nav.GetAttribute("order_id", "");
            Amount = nav.GetAttribute("amount", "");
            Currency = nav.GetAttribute("currency", "");
            XPathNavigator navigator = nav.SelectSingleNode("department");
            if(navigator != null)
            {
                DepartmentMerchantId = nav.GetAttribute("merchant_id", "");
                DepartmentAmount = nav.GetAttribute("amount", "");
            }
        }

        public string OrderId { get; set; }
        public string Amount { get; set; }
        public string Currency { get; set; }
        public string DepartmentMerchantId { get; set; }
        public string DepartmentAmount { get; set; }

        public string ToXmlString()
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Encoding = new ASCIIEncoding();
            settings.Indent = false;
            settings.OmitXmlDeclaration = true;

            StringBuilder result = new StringBuilder();
            using(XmlWriter writer = XmlWriter.Create(result, settings))
            {
                writer.WriteStartElement("order");
                writer.WriteAttributeString("order_id", OrderId);
                writer.WriteAttributeString("amount", Amount);
                writer.WriteAttributeString("currency", Currency);
                writer.WriteStartElement("department");
                writer.WriteAttributeString("merchant_id", DepartmentMerchantId);
                writer.WriteAttributeString("amount", DepartmentAmount);
                writer.WriteEndElement();
                writer.WriteEndElement();
            }
            return result.ToString();
            //return string.Format(FORMAT, OrderId, Amount, Currency, DepartmentMerchantId, DepartmentAmount);
        }
    }
}